/*******************************************************************************
 * Copyright (c) 2011 EclipseDevX.org and others.
 * 
 * All rights reserved. This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License v1.0 which
 * accompanies this distribution, and is available at:
 * 
 *     http://www.eclipse.org/legal/epl-v10.html
 *******************************************************************************/

package org.eclipsedevx.metrics.model;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 
 * 
 * @author dcarew
 */
public class MetricsData {
	public static final String JAVA_FILES = "java.files";
	public static final String JAVA_SLOC = "java.sloc";
	public static final String JAVA_CNC = "java.cnc";
	
	private Map<String, String> values = new HashMap<String, String>();
	
	public MetricsData() {
		
	}
	
	public MetricsData(String tokenValue) {
		if (tokenValue != null) {
			String[] strs = tokenValue.split(":");
			
			for (int i = 0; (i + 1) < strs.length; i += 2) {
				String key = strs[i];
				String value = strs[i + 1];
				
				values.put(key, value);
			}
		}
	}
	
	public Set<String> getKeys() {
		return values.keySet();
	}
	
	public String getValue(String key) {
		return values.get(key);
	}
	
	public int getValueAsInt(String key) {
		String value = values.get(key);
		
		if (value == null) {
			return 0;
		} else {
			return Integer.parseInt(value, 10);
		}
	}
	
	public double getValueAsDouble(String key) {
		String value = values.get(key);
		
		if (value == null) {
			return 0.0;
		} else {
			return Double.parseDouble(value);
		}
	}
	
	public void setValue(String key, String value) {
		values.put(key, value);
	}
	
	public void setValue(String key, int value) {
		values.put(key, Integer.toString(value, 10));
	}
	
	public void setValue(String key, double value) {
		values.put(key, Double.toString(value));
	}
	
	public String toStringToken() {
		StringBuilder builder = new StringBuilder();
		
		for (String key : values.keySet()) {
			String value = values.get(key);
			
			if (value != null) {
				if (builder.length() > 0) {
					builder.append(':');
				}
				
				builder.append(key);
				builder.append(':');
				builder.append(value);
			}
		}
		
		return builder.toString();
	}
	
	public String toString() {
		return toStringToken();
	}

    public void add(MetricsData data) {
        setValue(JAVA_FILES, getValueAsInt(JAVA_FILES) + data.getValueAsInt(JAVA_FILES));
        
        int sloc1 = getValueAsInt(JAVA_SLOC);
        int sloc2 = data.getValueAsInt(JAVA_SLOC);
        
        double cncWeight = sloc1 * getValueAsDouble(JAVA_CNC);
        cncWeight += sloc2 * data.getValueAsDouble(JAVA_CNC);
        
        setValue(JAVA_SLOC, sloc1 + sloc2);
        setValue(JAVA_CNC, cncWeight / (sloc1 + sloc2));
    }

    public void subtract(MetricsData data) {
        setValue(JAVA_FILES, getValueAsInt(JAVA_FILES) - data.getValueAsInt(JAVA_FILES));
        
        int sloc1 = getValueAsInt(JAVA_SLOC);
        int sloc2 = data.getValueAsInt(JAVA_SLOC);
        
        double cncWeight = sloc1 * getValueAsDouble(JAVA_CNC);
        cncWeight -= sloc2 * data.getValueAsDouble(JAVA_CNC);
        
        setValue(JAVA_SLOC, sloc1 - sloc2);
        setValue(JAVA_CNC, cncWeight / (sloc1 + sloc2));
    }

}
